package com.zh.index.btree.model;

/**
 * Created by Administrator on 2021/7/24.
 * 最小节点
 */
public  class DataNode<T extends  Comparable> implements Comparable<T> {

    protected T t;

    private LeafNode<T> parentNode;

    /**  下一个数据节点 **/
    protected DataNode<T> nextDataNode;

    /**  上一个数据节点 **/
    protected DataNode<T> preDataNode;

    public DataNode(T t) {
        this.t = t;
    }

    protected void setParentNode(LeafNode ln) {
        this.parentNode = ln;
    }

    /**
     * 再当前节点左边增加节点
     * @param node
     */
    protected void addLeftNode(DataNode node) {
        DataNode temp = this.preDataNode;
        node.preDataNode = temp;
        this.preDataNode = node;
        if (null != temp)
            temp.nextDataNode = node;
        node.nextDataNode = this;
    }

    /**
     * 再当前节点右边增加节点
     * @param node
     */
    protected void addRightDataNode(DataNode node) {
        DataNode temp = this.nextDataNode;
        node.nextDataNode = temp;
        this.nextDataNode = node;
        if (null != temp)
            temp.preDataNode = node;
        node.preDataNode = this;
    }

    protected void delete() {
        DataNode temp = this.preDataNode;
        if (null != temp)
            temp.nextDataNode = this.nextDataNode;
        if (null != temp.nextDataNode)
            temp.nextDataNode.preDataNode = temp;

        this.preDataNode = null;
        this.nextDataNode = null;
    }

    @Override
    public int compareTo(T o) {
        return this.t.compareTo(o);
    }

    @Override
    public String toString() {
        return t.toString();
    }

    public LeafNode<T> getParentNode() {
        return this.parentNode;
    }

    public T getT() {
        return t;
    }
}
